﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ComponentModel;

namespace WebPALTT.Lib
{
    /*
     * Class: ActionItem_Variable
     * 
     * This class represents a create variable action and inherits from @link[ActionItem.cs.htm, ActionItem].
     * 
     * ++{{NB:}} Changes to a create variable action during runtime will not alter the runtime behavior.++
     * ++The action is compiled at the start of a test.++
     */
    [Serializable()]
    public class ActionItem_Variable : ActionItem
    {
        public override ActionType ActionType { get { return ActionType.Variable; } }

        string mType;
        bool mStatic = false;
        string mValue = null;

        /// <summary>
        /// Gets or sets the type of the variable to create.
        /// NB: While any object type that can created from a script action in the same scenario can be used, its recomended to only use primitive types.
        /// </summary>
        [DescriptionAttribute("The type of the variable.")]
        public string Type { get { return mType; } set { mType = value; } }
        /// <summary>
        /// Gets or sets if the variable is a static variable.
        /// </summary>
        [DescriptionAttribute("Indicates whether the variable is static.")]
        public bool IsStatic { get { return mStatic; } set { mStatic = value; } }
        /// <summary>
        /// Gets or sets the intial value of the variable.
        /// </summary>
        [DescriptionAttribute("The value of the variable")]
        public string Value { get { return mValue; } set { mValue = value; } }

        public ActionItem_Variable(string name) : this(name, "string", false, null) { }

        public ActionItem_Variable(string name, string type, bool isStatic, string value)
            : base(name)
        {
            mType = type;
            mStatic = isStatic;
            mValue = value;
        }

        public ActionItem_Variable(SerializationInfo info, StreamingContext ctxt)
            : base(info, ctxt)
        {
            mType = (string)info.GetValue("Type", typeof(string));
            mStatic = (bool)info.GetValue("Static", typeof(bool));
            mValue = (string)info.GetValue("Value", typeof(string));
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            base.GetObjectData(info, ctxt);
            info.AddValue("Type", mType);
            info.AddValue("Static", mStatic);
            info.AddValue("Value", mValue);
        }

        public override string[] Validate()
        {
            List<string> errors = new List<string> { };
            if (Name.Equals("Then") || Name.Equals("Else") || Name.Equals("Actions") || Name.Equals("Then"))
                errors.Add("Error: action[" + Name + "(" + ActionType + ")] is using a reserverd name.");

            if (Name.IndexOfAny(new char[] { ' ', '(', ')', '[', ']', '{', '}', ';', ':', '.', ',', '\\', '/' }) > -1)
                errors.Add("Error: action[" + Name + "(" + ActionType + ")].Name contains invalid characters.");

            return errors.ToArray();
        }

        public override ActionItem GetChild(Guid key) { return null; }
    }    
}
